package huzh.foundation.dao;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

/**
 *<p>title:DbResults</p>
 *<p>description:</p>
 * @since 2014-6-7
 * @author huzh
 * @version 1.0
 */
public class DbResults implements Serializable{
    /**serialVersionUID*/
    private static final long serialVersionUID = -4779863211012888449L;
    /**connection*/
    private Connection connection;
    /**productName*/
    private String productName;
    /**productVersion*/
    private String productVersion;
    /**resultSet*/
    private ResultSet resultSet;
    /**columnCount*/
    private int columnCount;
    /**columnNames*/
    private String[] columnNames;
    /**queryResults*/
    private Vector<Object> queryResults;
    /**rowData*/
    @SuppressWarnings("unused")
    private String[] rowData;

    /**
     * @param connection
     * @param productName
     * @param productVersion
     * @param resultSet
     * @param columnCount
     * @param columnNames
     */
    public DbResults(Connection connection, String productName, String productVersion, ResultSet resultSet, 
            int columnCount, String[] columnNames){
        this.connection = connection;
        this.productName = productName;
        this.productVersion = productVersion;
        this.resultSet = resultSet;
        this.columnCount = columnCount;
        this.columnNames = columnNames;
        this.rowData = new String[columnCount];
        this.queryResults = new Vector<Object>();
    }

    /**
     * @return
     */
    public Connection getConnection() {
        return this.connection;
    }

    /**
     * @return
     */
    public String getProductName() {
        return this.productName;
    }

    /**
     * @return
     */
    public String getProductVersion() {
        return this.productVersion;
    }

    /**
     * @return
     */
    public ResultSet getResultSet() {
        try {
            this.resultSet.beforeFirst();
        }catch (SQLException sqle) {
            System.err.println(sqle.getMessage());
        }
        return this.resultSet;
    }

    /**
     * @return
     */
    public int getColumnCount() {
        return this.columnCount;
    }

    /**
     * @return
     */
    public String[] getColumnNames() {
        return this.columnNames;
    }

    /**
     * @return
     */
    public int getRowCount() {
        return this.queryResults.size();
    }

    /**
     * @return
     */
    public boolean isEmpty() {
        return getRowCount() == 0;
    }

    /**
     * @param index
     * @return
     */
    public String[] getRow(int index) {
        return (String[])(String[])this.queryResults.elementAt(index);
    }

    /**
     * @param row
     */
    public void addRow(String[] row) {
        this.queryResults.addElement(row);
    }

    /**
     * @param row
     * @param col
     * @return
     */
    public String getField(int row, int col) {
        return getRow(row)[col];
    }

    /**
     * @param row
     * @param fieldName
     * @return
     */
    public String getField(int row, String fieldName) {
        int col = 0;
        while ((!this.columnNames[col].equalsIgnoreCase(fieldName.trim())) && (col < this.columnCount)){
            col++;
        }
        if (col < this.columnCount) {
            return getField(row, col);
        }
        return null;
    }

    /**
     * @param headingColor
     * @return
     */
    public String toHTMLTable(String headingColor){
        StringBuffer sb = new StringBuffer("<TABLE BORDER=1>\n");
        if (headingColor != null) {
            sb.append("<TR BGCOLOR=\"" + headingColor + "\">\n");
        } else {
            sb.append("<TR>\n");
        }
        for (int i = 0; i < getColumnCount(); i++) {
            sb.append("<TD>" + this.columnNames[i]);
        }
        for (int row = 0; row < getRowCount(); row++) {
            sb.append("\n <TR>\n");
            String[] rowData = getRow(row);
            for (int col = 0; col < getColumnCount(); col++) {
                sb.append("<TD>" + rowData[col]);
            }
        }
        sb.append("\n</TABLE>");
        return sb.toString();
    }

    /**
     * @param entries
     * @param entryWidth
     * @return
     */
    private String makeRow(String[] entries, int entryWidth) {
        StringBuffer row = new StringBuffer();
        row.append("|");
        for (int i = 0; i < entries.length; i++) {
            row.append(padString(entries[i].trim(), entryWidth, " "));
            row.append(" |");
        }
        return row.toString();
    }

    /**
     * @param entryWidth
     * @param columnCount
     * @return
     */
    private String makeSeparator(int entryWidth, int columnCount) {
        String entry = padString("", entryWidth + 1, "-");
        StringBuffer separator = new StringBuffer("+");
        for (int i = 0; i < columnCount; i++) {
            separator.append(entry);
            separator.append("+");
        }
        return separator.toString();
    }

    /**
     * @param orig
     * @param size
     * @param padChar
     * @return
     */
    private String padString(String orig, int size, String padChar) {
        if (orig == null) {
            orig = "<null>";
        }
        StringBuffer buffer = new StringBuffer("");
        int extraChars = size - orig.length();
        for (int i = 0; i < extraChars; i++) {
            buffer.append(padChar);
        }
        buffer.append(orig);
        return buffer.toString();
    }

    /**
     * @param queryName
     * @param entryWidth
     * @param printMetaData
     */
    public void PrintQueryResults(String queryName, int entryWidth, boolean printMetaData){
        if (getResultSet() == null) {
            return;
        }
        if (printMetaData) {
            System.out.println("Database: " + getProductName());
            System.out.println("Version: " + getProductVersion());
            System.out.println();
        }
        System.out.println(queryName + ": ");
        String underline = padString("", queryName.length() + 1, "=");
        System.out.println(underline);
        String separator = makeSeparator(entryWidth, getColumnCount());
        System.out.println(separator);
        String row = makeRow(getColumnNames(), entryWidth);
        System.out.println(row);
        System.out.println(separator);
        for (int i = 0; i < getRowCount(); i++) {
                row = makeRow(getRow(i), entryWidth);
                System.out.println(row);
        }
        System.out.println(separator);
    }
}
